//- Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. 
//- <https://www.chatopera.com>, Licensed under the Chunsong Public 
//- License, Version 1.0  (the "License"), https://docs.cskefu.com/licenses/v1.html
//- Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
//- Copyright (C) 2018-Jun. 2023 Chatopera Inc, <https://www.chatopera.com>
//- Licensed under the Apache License, Version 2.0
//- http://www.apache.org/licenses/LICENSE-2.0
extends /admin/include/layout.pug

block content
    style.
        .ztree li span {
            display: inline-block !important;
            white-space: pre-wrap;
            width: 170px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    .row(style='height:100%;')
        .col-lg-3(style='border-right:1px solid #e6e6e6;padding-right:0px;')
            h1.site-h1(style='background-color:#FFFFFF;')
                | #{currentOrgan.name}
                span(style='float:right;')
                    button.layui-btn.layui-btn-small.green#areaadd(href='/admin/organ/add.html?' + (organData ? 'parent=' + organData.id : ''), data-toggle='ajax', data-height='400', data-width='650', data-title='创建部门')
                        | 创建部门
            .row(style='padding:5px;')
                .col-lg-12
                    ul#organTree.ztree
        span.col-lg-9
            h1.site-h1(style='background-color:#FFFFFF;')
                span(style='width:400px;display:inline-block;;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;')
                    if organData
                        | #{organData.name}
                    else
                        | 默认机构
                    if userList
                        | (#{userList.size()})
                span(style='float:right;')
                    if organData
                        button.layui-btn.layui-btn-small.green(href="/admin/organ/seluser.html?organ=" + organData.id, data-toggle="ajax", data-width="750", data-title="添加用户到当前部门")
                            | 添加用户到当前部门
                        button.layui-btn.layui-btn-original.layui-btn-small(href="/admin/organ/area.html?id=" + organData.id, data-toggle="ajax", data-width="450", data-height="400", data-title="地区设置")
                            | 地区设置
                        button.layui-btn.layui-btn-original.layui-btn-small(href="/admin/organ/edit.html?id=" + organData.id, data-toggle="ajax", data-width="650", data-height="400", data-title="修改部门")
                            | 修改部门
                        button.layui-btn.layui-btn-danger.layui-btn-small(href="/admin/organ/delete.html?id=" + organData.id, data-toggle="tip", data-title="请确认是否删除该部门？")
                            | 删除部门

            // 展示部门根用户
            .row(style='padding:5px;')
                .col-lg-12
                    blockquote.layui-elem-quote 部门根用户
                    table.layui-table(lay-skin='line')
                        colgroup
                            col(width='30%')
                            col(width='20%')
                            col(width='25%')
                            col(width='24%')
                            col(width='1%')
                            col
                        thead
                            tr
                                th 用户
                                th 姓名
                                th 电子邮件
                                th 手机
                                th(style='white-space:nowrap;', nowrap) 操作
                        tbody(style='table-layout: fixed; word-break: break-all;')
                            if size(userList) > 0
                                for organUser in userList
                                    tr
                                        td(style="width:150px;")
                                            a(href="")
                                                | #{organUser.username}
                                        td(style="width: 100px")= organUser.uname
                                        td= organUser.email
                                        td= organUser.mobile
                                        td(style="white-space: nowrap;")
                                            a(href="/admin/organ/user/delete.html?id=" + organUser.id + "&organ=" + organData.id, data-toggle="tip", data-title="请确认是否从部门【#{organData.name}】中移除用户【#{organUser.username}】？")
                                                i.layui-icon(style="color:red;") ဆ
                                                | 移除
                            else
                                td(colspan="5", style="height:170px;")
                                    .ukefu-empty(style="background: none;")
                                        i.layui-icon 
                                        div(style="") 还没有部门根用户
            .row(style='padding:5px;')
                .col-lg-12#page(style='text-align:center;')

            // 展示部门附属的用户
            hr
            .row(style='padding:5px;')
                .col-lg-12
                    blockquote.layui-elem-quote 附属部门用户
                    table.layui-table(lay-skin='line')
                        colgroup
                            col(width='15%')
                            col(width='15%')
                            col(width='15%')
                            col(width='20%')
                            col(width='20%')
                            col(width='1%')
                            col
                        thead
                            tr
                                th 部门
                                th 用户
                                th 姓名
                                th 电子邮件
                                th 手机
                                th(style='white-space:nowrap;', nowrap) 操作
                        tbody(style='table-layout: fixed; word-break: break-all;')
                            if size(affiliateUsers) > 0
                                for organUser in affiliateUsers
                                    tr
                                        td(style="width:100px;")= organUser.currOrganName
                                        td(style="width:100px;")
                                            a(href="")
                                                | #{organUser.username}
                                        td(style="width: 100px")= organUser.uname
                                        td= organUser.email
                                        td= organUser.mobile
                                        td(style="white-space: nowrap;")
                                            a(href="/admin/organ/user/delete.html?id=" + organUser.id + "&organ=" + organUser.currOrganId, data-toggle="tip", data-title="请确认是否从部门【#{organUser.currOrganName}】中移除用户【#{organUser.username}】？")
                                                i.layui-icon(style="color:red;") ဆ
                                                | 移除
                            else
                                td(colspan="5", style="height:170px;")
                                    .ukefu-empty(style="background: none;")
                                        i.layui-icon 
                                        div(style="") 还没有附属部门用户


    script(type='text/javascript').
        var setting = {
            data: {simpleData: {enable: true}},
            callback: {onClick: onClick}
        };
        function onClick(e, treeId, treeNode) {
            if (treeNode.topic) {
                location.href = "/admin/organ/index.html?organ=" + treeNode.id;
            } else if (treeNode.id == "0") {
                location.href = "/admin/organ/index.html";
            } else if (treeNode.area) {
                $('#areaadd').attr("href", "/admin/organ/add.html?area=" + treeNode.id);
            }
        }
        var zNodes = !{pugHelper.toJSON(organList)}
        $.each(zNodes, function (i, o) {
            o.id = o.id;
            o.pId = o.parent ? o.parent : '0';
            o.name = o.name;
            o.open = true;
            o.topic = true;
            o.icon = (o.area && o.area != "") ? '/images/area.png' : '/images/dept.png'
        });
        $(document).ready(function () {
            var ztree = $.fn.zTree.init($("#organTree"), setting, zNodes);
            if ('#{organData}') {
                var node = ztree.getNodeByParam('id', "#{organData.id}");//获取id为1的点
                ztree.selectNode(node);//选择点 // </#if>
            }
            // var x = document.getElementById("node_Name");
            // var y = x.getElementsByClassName("child");
            // var i;
            // for (i = 0; i < y.length; i++) {
            // y[i].style.backgroundColor = "red";
            // }
        });
        layui.use('layer', function () {
            var layer = layui.layer;
            var msg = '#{msg}';
            if (msg == 'admin_organ_update_name_not') {
                layer.msg('组织机构分类名已存在！', {icon: 2, time: 2000})
            } else if (msg == 'admin_oran_not_delete') {
                layer.msg('无法删除，因为存在子组织', {icon: 2, time: 2000})
            } else if (msg == 'admin_oran_not_empty') {
                layer.msg('无法删除，因为组织下存在用户', {icon: 2, time: 2000})
            } else if (msg == 'admin_organ_update_success') {
                layer.msg('修改组织成功', {icon: 1, time: 2000})
            } else if (msg == 'admin_organ_new_success') {
                layer.msg('新建组织成功', {icon: 1, time: 2000})
            } else if (msg == 'admin_organ_update_not_standard') {
                layer.msg('修改无法完成，上级机构选择错误', {icon: 2, time: 2000})
            } else if (msg == 'not_allow_remove_user') {
                layer.msg('用户只有一个组织，不允许移除', {icon: 2, time: 2000})
            }
        });

